package com.example.validate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminDAO {
	 static Connection currentCon = null;
	   static ResultSet rs = null;  
		
	   public static boolean addSchool(String fullName, String shortName, String place, String zip, String country) {
		   boolean good = true;
	      //preparing some objects for connection 
	      PreparedStatement pst = null;      
		    
	      String sqlQuery = "INSERT INTO school (school_id, full_name, short_name, place, zip, country)" +
	      		" VALUES(null, ?, ?, ?, ?, ?)";
	    		 
		    
		   try {
		      //connect to DB 
		      currentCon = ConnectionManager.getConnection();
		      currentCon.setAutoCommit(false);
		    //creating the statement
		      pst = currentCon.prepareStatement(sqlQuery);
		      pst.setString(1, fullName);
		      pst.setString(2, shortName);
		      pst.setString(3, place);
		      pst.setString(4, zip);
		      pst.setString(5, country);
		      
		      //execute the statement
		      pst.execute();
		    
		      currentCon.commit();
		      
		   } catch (Exception ex) {
			   good = false;
			   System.out.println("Adding country failed: An Exception has occurred! " + ex);
		   } 
		    //some exception handling
		   finally {
		      if (pst != null) {
		         try {
		            pst.close();
		         } catch (Exception e) {}
		            pst = null;
		         }
			
		      if (currentCon != null) {
		         try {
		            currentCon.close();
		         } catch (Exception e) {
		         }
		
		         currentCon = null;
		      }
		   }

		   return good;
	   }
	   
	   
	   public static boolean addCountry(String shortName, String fullName){
		   boolean good = true;
		   PreparedStatement pst = null;
		   String sqlQuery = "INSERT INTO country (short_name, full_name) VALUES(?, ?)";
		   
		   try{
			   currentCon = ConnectionManager.getConnection();
			   currentCon.setAutoCommit(false);
			   pst = currentCon.prepareStatement(sqlQuery);
			   pst.setString(1, shortName);
			   pst.setString(2, fullName);
			   
			   pst.execute();
			
			   
			   currentCon.commit();
		   }catch(Exception e){
			   good = false;
		   }finally {
		      if (pst != null) {
		         try {
		            pst.close();
		         } catch (Exception e) {}
		            pst = null;
		         }
			
		      if (currentCon != null) {
		         try {
		            currentCon.close();
		         } catch (Exception e) {
		         }
		
		         currentCon = null;
		      }
			   }
		   
		   
		   return good;  
		   
	   }
	   
	   
	   public static boolean removeUser(String name){
		   boolean good = true;
		   PreparedStatement pst = null;
		   String sqlQuery = "DELETE FROM users WHERE name = ?";
		   
		   try{
			   currentCon = ConnectionManager.getConnection();
			   currentCon.setAutoCommit(false);
			   pst = currentCon.prepareStatement(sqlQuery);
			   pst.setString(1,  name);
			   pst.execute();
			   
			   currentCon.commit();
			   
			   
		   }catch(Exception e){
			   good = false;
			   e.printStackTrace();
		   }finally {
		      if (pst != null) {
			         try {
			            pst.close();
			         } catch (Exception e) {}
			            pst = null;
			         }
				
			      if (currentCon != null) {
			         try {
			            currentCon.close();
			         } catch (Exception e) {
			         }
			
			         currentCon = null;
			      }
				 }
		   
		   
		   return good;
	   }
}
